package com.ygo.feihua;

import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.ClipboardManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import com.ygo.feihua.NewPaibiao;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Calendar;
import android.util.Log;
import java.util.*;
import com.ygo.feihua.view.*;

public class NewPaibiao extends AppCompatActivity
{
	String zhan1,zhan2,shijian,didian,guize,lun;
	Button 获时,二轮;
	private Spinner 规选;
	EditText 规则;
	AutoCompleteTextView edittext1,edittext2;
	Gongju gj;
	@Override
    public void onCreate(Bundle savedInstanceState)
	{

        super.onCreate(savedInstanceState);
		 
		
        // Set main.xml as user interface layout
        setContentView(R.layout.paibiao);
		final EditText edittext= (EditText) findViewById(R.id.地点);
		edittext1=(AutoCompleteTextView) findViewById(R.id.战队1);
		edittext2=(AutoCompleteTextView) findViewById(R.id.战队2);

		final EditText edittext3=(EditText)findViewById(R.id.一队);
		final EditText edittext4=(EditText)findViewById(R.id.二队);
		final EditText edittext5=(EditText)findViewById(R.id.时间);
		final EditText edittext6=(EditText)findViewById(R.id.复制框);
		规则=(EditText) findViewById(R.id.规则);
		获时=(Button) findViewById(R.id.获时);
		Button button= (Button) findViewById(R.id.重置);
		Button button1=(Button)findViewById(R.id.排表);
		Button button3=(Button)findViewById(R.id.复制按钮);
		规选=(Spinner) findViewById(R.id.规选);
		二轮=(Button) findViewById(R.id.二轮);

		gj=Gongju.getdx(this);
		gj.cshToolbar((Toolbar) findViewById(R.id.toolbar),"排表");
		//OurBar ourbar=(OurBar)findViewById(R.id.ourbar);
		
		//setSupportActionBar(ourbar);
		//ourbar.initMenu();
		
		File f=new File(Environment.getExternalStorageDirectory().toString()+"/ygo助手/TeamList.txt");
		
		String aa="";
		try{
		BufferedReader reader=new BufferedReader(new InputStreamReader(new FileInputStream(f)));
		String line="";
		
			for (;(line = reader.readLine()) != null;)
			{
				aa += line +",";
			}
		}
		catch (IOException e)
		{}

		
		
		String[] 战提=aa.split(",");
		
		ArrayAdapter<String> a=new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,战提);
		edittext1.setAdapter(a);
		edittext2.setAdapter(a);
		
		
		规选.setOnItemSelectedListener(new OnItemSelectedListener(){

				@Override
				public void onItemSelected(AdapterView<?> p1, View p2, int p3, long p4)
				{
					// TODO: Implement this method
					
					if(p3==0){
						规则.setText("2/3【KOF】");
						lun="------第二轮------";
					}
					else if(p3==1){
						规则.setText("人头赛");
						lun="";
					}
				}

				@Override
				public void onNothingSelected(AdapterView<?> p1)
				{
					// TODO: Implement this method
				}
			});
	
		
		
	
		

		button.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View p1)
				{
					edittext.setText("");
					edittext1.setText("");
					edittext2.setText("");
					edittext3.setText("");
					edittext4.setText("");
					edittext5.setText("");
					edittext6.setText("");
					//重置
				}


			});
			
		二轮.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View p1)
				{
					String ss="";
					List<String>[] ls=piPei(edittext3.getText().toString().trim(),edittext4.getText().toString().trim(),null);
					for(int i=0;i<ls[0].size();i++){
						ss+=ls[0].get(i)+"\t  0:0  \t"+ls[1].get(i)+"\n";
					}
		
					//String 匹配清单2=PiPei.匹配(edittext3.getText().toString().trim(), edittext4.getText().toString().trim());
					edittext6.setText(ss+"------第三轮------");
					
					
					// TODO: Implement this method
				}
			});
		button1.setOnClickListener(new OnClickListener(){
				@Override
				public void onClick(View p2)
				{
					shijian = edittext5.getText().toString().trim();
					didian = edittext.getText().toString().trim();
					zhan1 = edittext1.getText().toString().trim();
					zhan2 = edittext2.getText().toString().trim();
					guize=规则.getText().toString().trim();
					//获取输入的值
					//String 匹配清单1=PiPei. 匹配(edittext3.getText().toString().trim(), edittext4.getText().toString().trim());
					//生成清单，传入一队，二队数据 注:trim()是除去字符串左右空格
			
					String ss="";
					List<String>[] ls=piPei(edittext3.getText().toString().trim(),edittext4.getText().toString().trim(),null);
					for(int i=0;i<ls[0].size();i++){
						ss+=ls[0].get(i)+"\t  0:0  \t"+ls[1].get(i)+"\n";
					}
				
					
					edittext6.setText("战队：" + zhan1 + " VS " + zhan2 +  "\n时间：" + shijian + "\n规则："+guize+"\n地点：" + didian + "\n------第一轮------\n" + ss + lun);
				}
			});

		button3.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View p1)
				{
					ClipboardManager cmb = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
					cmb.setText(edittext6.getText().toString());//复制命令
					gj.show("已复制到剪贴板");// 提示
				}
			});
			
			
			
		获时.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View p1)
				{
							int year=0;//年
							int month=0;//月
							int day=0;//日
							Calendar c=Calendar.getInstance();//获得系统当前日期
							year=c.get(Calendar.YEAR);
							month=c.get(Calendar.MONTH)+1;//系统日期从0开始算起
							day=c.get(Calendar.DAY_OF_MONTH);
							edittext5.setText(year+"."+month+"."+day);
							//输出时间到时间框里
				}
			});


	
    }
	
	
	public static List<String>[] piPei(String s1,String s2,String[] regexs) {
		@SuppressWarnings("unchecked")
			List<String>[] lists=new List[2];
		StringBuilder regex=new StringBuilder();
		if(regexs==null)
			regex.append("\\s*,\\s*|\\s*，\\s*|\\s+");
		else {
			for(String s:regexs){
				regex.append("\\s*");
				regex.append(s);
				regex.append("\\s*|");
			}
			if(regex.toString().endsWith("|"))
				regex.delete(regex.length()-1,regex.length());
		}
		String[] ss=s1.split(regex.toString());
		List<String> list1=new ArrayList<String>();
		for(int i=0;i<ss.length;i++){
			if(!"".equals(ss[i])){
				list1.add(ss[i]);
			}
		}
		ss=s2.split(regex.toString());
		List<String> list2=new ArrayList<String>();
		for(int i=0;i<ss.length;i++){
			if(!"".equals(ss[i])){
				list2.add(ss[i]);
			}
		}
		Collections.shuffle(list1);
		Collections.shuffle(list2);
		int maxLen=list1.size()>list2.size()?list1.size():list2.size();
		for(int i=0;i<maxLen;i++){
			if(i==list1.size())
				list1.add("轮空");
			if(i==list2.size())
				list2.add("轮空");
		}
		lists[0]=list1;
		lists[1]=list2;
		return lists;
	}
	
}





/**
 判断字符串中是否有指定字符
 */
class 次数
{
	public static boolean cunZai(int[] ii, int y)
	{
		int g=0;
		for (int i=0;i < ii.length;i++)
		{
			if (ii[i] == y)
			{
				g++;
			}
		}
		if (g == 0)
		{
			return false;
		}
		else
		{
			return true;
		}
	}
}
 class PiPei
{
	public static  String 匹配(String z1, String z2)
	{
		while (z1.endsWith(","))
		{
			z1 = z1.substring(0, z1.length() - 1);
		}
		while (z1.endsWith(","))
		{
			z2 = z2.substring(0, z2.length() - 1);
		}
		while (z1.startsWith(","))
		{
			z1 = z1.substring(0, z1.length() - 1);
		}
		while (z1.startsWith(","))
		{
			z2 = z2.substring(0, z2.length() - 1);
		}//去除前后多余","
		String 匹配清单="";
		String [] q=z1.split(",");
		String [] w=z2.split(",");
		int a=q.length;
		int b=w.length;
		if (a != b)
		{
			if (a > b)
			{
				for (int i=0;i < a - b;i++)
				{
					z2 = z2 + ",♦";
				}
			}
			else
			{
				for (int i=0;i < b - a;i++)
				{
					z1 = z1 + ",♦";
				}
			}
		}//当两队人数不等时添加空队员
		String [] aa=z1.split(","+"+");
		String [] bb=z2.split(","+"+");//提取队员
		int []pi1=new int[aa.length];
		int []pi2=new int[aa.length];
		for (int ww=0;ww < pi1.length;ww++)
		{
			pi1[ww] = -1;
		}
		for (int ww=0;ww < pi2.length;ww++)
		{
			pi2[ww] = -1;
		}//初始化随机数组
		int gg=0;
		int hh=0;
		while (次数.cunZai(pi1, -1))
		{
			int in=(int)(Math.random() * pi1.length);
			while (次数.cunZai(pi1, in))
			{
				in = (int)(Math.random() * pi1.length);
			}
			pi1[gg] = in;
			gg++;
		}
		while (次数.cunZai(pi2, -1))
		{
			int in=(int)(Math.random() * pi2.length);
			while (次数.cunZai(pi2, in))
			{
				in = (int)(Math.random() * pi2.length);
			}
			pi2[hh] = in;
			hh++;
		}//随机数组赋值
		for (int iii=0;iii < pi1.length;iii++)
		{
			if (aa[pi1[iii]].equals("♦") & !(bb[pi2[iii]].equals("♦")))
			{
				匹配清单 = 匹配清单 + bb[pi2[iii]] + "轮空" + "\n";
			}
			else if (bb[pi2[iii]].equals("♦") & !(aa[pi1[iii]].equals("♦")))
			{
				匹配清单 = 匹配清单 + aa[pi1[iii]] + "轮空" + "\n";
			}
			else if (aa[pi1[iii]].equals("♦") & bb[pi2[iii]].equals("♦"))
			{}
			else
			{
				匹配清单 = 匹配清单 + aa[pi1[iii]] + "  0:0  " + bb[pi2[iii]] + "\n";
			}
		}//通过随机数组生成匹配清单
		return 匹配清单;
	}
	
	
	
}
